﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cesave.Domain.PermisosModule.Entities;
using System.ComponentModel;
using Cesave.Domain.PermisosModule.Contracts.Secciones;
using Cesave.Infrastructure.CrossCutting.IoC;
using System.Windows.Forms;
using Cesave.Domain.Core.Entities;

namespace Cesave.DistributedServices.PermisosModule.WindowsHost.ViewModels.Catalogs.Secciones
{
	public partial class SeccionesViewModel
	{
		#region Private properties

		/// <summary>
		/// 
		/// </summary>
		private List<Seccion> _secciones;

		/// <summary>
		/// 
		/// </summary>
		private Seccion _seccion;

		#endregion

		#region Public properties

		/// <summary>
		/// 
		/// </summary>
		public List<Seccion> Secciones
		{
			get
			{
				return _secciones;
			}
			private set
			{
				_secciones = value;
				if (_secciones != null)
					RaisePropertyChanged("Secciones");
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public Seccion Seccion
		{
			get
			{
				return _seccion;
			}
			private set
			{
				_seccion = value;
				if (_seccion != null)
					RaisePropertyChanged("Seccion");
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public bool IsSeccionReady
		{
			get
			{
				return Seccion != null;
			}
		}

		#endregion

		#region Private methods

		/// <summary>
		/// 
		/// </summary>
		private void LoadSecciones()
		{
			StartWaiting();
			using (BackgroundWorker worker = new BackgroundWorker())
			{
				worker.DoWork += (sender, e) =>
				{
					ISeccionService service = IoCFactory.Resolve<ISeccionService>();
					e.Result = service.FindSeccionesByModulo(Modulo.ModuloGuid);
				};
				worker.RunWorkerCompleted += (sender, e) =>
				{
					EndWaiting();
					if (!e.Cancelled && e.Error == null)
						Secciones = e.Result as List<Seccion>;
					else if (e.Error != null)
						ShowMessage(e.Error.Message);
				};
				worker.WorkerSupportsCancellation = true;
				worker.RunWorkerAsync();
			}
		}

		#endregion

		#region Event methods

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void AddNewSeccion(object sender, EventArgs e)
		{
			_view.seccionesBindingSource.AddingNew += AddingNewSeccion;
			_view.seccionesBindingSource.AddNew();
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void AddingNewSeccion(object sender, AddingNewEventArgs e)
		{
			Seccion = new Seccion
			{
				DistritoGuid = Junta.DistritoGuid,
				DistritoId = Junta.DistritoId,
				EstadoGuid = Junta.EstadoGuid,
				EstadoId = Junta.EstadoId,
				JuntaGuid = Junta.JuntaGuid,
				JuntaId = Junta.JuntaId,
				ModuloGuid = Modulo.ModuloGuid,
				ModuloId = Modulo.ModuloId,
				MunicipioGuid = Junta.MunicipioGuid,
				MunicipioId = Junta.MunicipioId,
				SeccionGuid = Guid.NewGuid(),
				SeccionId = 0,
				Nombre = string.Empty,
				Status = true,
			};
			Seccion.MarkAsAdded();
			e.NewObject = Seccion;
			FocusDefaultControl();
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void SaveSecciones(object sender, EventArgs e)
		{
			StartWaiting();
			using (BackgroundWorker worker = new BackgroundWorker())
			{
				worker.DoWork += (s, args) =>
				{
					ISeccionService service = IoCFactory.Resolve<ISeccionService>();
					Secciones.ForEach(seccion =>
					{
						if (seccion.ChangeTracker.State == ObjectState.Added)
							service.AddSeccion(seccion);
						else
							service.ChangeSeccion(seccion);
						seccion.AcceptChanges();
					});
				};
				worker.RunWorkerCompleted += (s, args) =>
				{
					EndWaiting();
					if (!args.Cancelled && args.Error == null)
						LoadSecciones();
					else if (args.Error != null)
						ShowMessage(args.Error.Message);
				};
				worker.WorkerSupportsCancellation = true;
				worker.RunWorkerAsync();
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void SetCurrentSeccion(object sender, EventArgs e)
		{
			BindingSource bindingSource = sender as BindingSource;
			if ((bindingSource != null) && (bindingSource.Current != null))
				Seccion = bindingSource.Current as Seccion;
		}

		/// <summary>
		/// 
		/// </summary>
		private void OnSeccionesPropertyChanged()
		{
			_view.seccionesBindingSource.DataSource = Secciones;
			_view.seccionesBindingSource.CurrentChanged += SetCurrentSeccion;
			SetCurrentSeccion(_view.seccionesBindingSource, new EventArgs());
		}

		/// <summary>
		/// 
		/// </summary>
		private void OnSeccionPropertyChanged()
		{
			
		}

		#endregion

	}
}